﻿using ERP.Domain.Products;
using Product.API.ISerivces.Bom;
using Product.API.ISerivces.DTOS;
using Repository.communal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Product.API.Serivces.Bom
{
    /// <summary>
    /// Bom管理服务实现类
    /// </summary>
    public class BomSerivces : IBomSerivces
    {
        /// <summary>
        /// Bom仓储
        /// </summary>
        private readonly IBaseRepository<BomInfo> bomRepository;
        private readonly IBaseRepository<MaterialStructure> materialStructureRepository;
        private readonly IBaseRepository<ProductMaterial> productMaterialRepository;




        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="bomRepository">Bom仓储</param>
        public BomSerivces(IBaseRepository<BomInfo> bomRepository, IBaseRepository<MaterialStructure> materialStructureRepository, IBaseRepository<ProductMaterial> productMaterialRepository)
        {
            this.bomRepository = bomRepository;
            this.materialStructureRepository = materialStructureRepository;
            this.productMaterialRepository = productMaterialRepository;
        }

        /// <summary>
        /// 物料配件批量添加
        /// </summary>
        /// <param name="materialStructuer">物料配件</param>
        /// <returns>返回受影响函数</returns>
        public async Task<int> BatchCreateMaterial(List<BatchCreateMaterialStructuerDTO> materialStructuer, Guid BomNumber)
        {
            var entity = materialStructuer.Select(x => new MaterialStructure
            {
                ParentProductNumber = x.ParentProductNumber,
                ProductMaterialName = x.ProductMaterialName,
                ChildNumber = x.ChildNumber,
                BomNumber = BomNumber
            }).ToList();

            var data = await materialStructureRepository.BatchAdd(entity);
            return data;
        }

        /// <summary>
        /// 产品用料批量添加
        /// </summary>
        /// <param name="productMaterial">产品物料</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> BatchCreateProductMaterial(List<BatchCreateProductMaterialDTO> productMaterial, Guid ProductNumber, string SerialNumber)
        {
            var entity = productMaterial.Select(x => new ProductMaterial
            {
                MaterialNumber = x.MaterialNumber,
                MaterialName = x.MaterialName,
                SpecificationModel = x.SpecificationModel,
                Unit = x.Unit,
                UsageQuantity = x.UsageQuantity,
                MaterialRatio = x.MaterialRatio,
                ProductNumber = ProductNumber,
                SerialNumber = SerialNumber
            }).ToList();

            var data = await productMaterialRepository.BatchAdd(entity);
            return data;
        }

        /// <summary>
        /// 物料配件批量修改
        /// </summary>
        /// <param name="materialStructuer">物料配件</param>
        /// <param name="BomNumber">BOM编号</param>
        /// <returns>返回受影响函数</returns>
        public async Task<int> BatchUpdateMaterial(List<BatchUpdateMaterialStructuerDTO> materialStructuer, Guid BomNumber)
        {
            var entity = materialStructuer.Select(x => new MaterialStructure
            {
                Id = x.Id,
                ParentProductNumber = x.ParentProductNumber,
                ProductMaterialName = x.ProductMaterialName,
                ChildNumber = x.ChildNumber,
                BomNumber = BomNumber,
                IsDelete = true
            }).ToList();

            var data = await materialStructureRepository.BatchUpdate(entity);
            return data;
        }

        /// <summary>
        /// 产品用料批量修改
        /// </summary>
        /// <param name="productMaterial">产品物料</param>
        /// <param name="ProductNumber">产品编号</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> BatchUpdateProductMaterial(List<BatchUpdateProductMaterialDTO> productMaterial, Guid ProductNumber, string SerialNumber)
        {
            var entity = productMaterial.Select(x => new ProductMaterial
            {
                Id = x.Id,
                MaterialNumber = x.MaterialNumber,
                MaterialName = x.MaterialName,
                SpecificationModel = x.SpecificationModel,
                Unit = x.Unit,
                UsageQuantity = x.UsageQuantity,
                MaterialRatio = x.MaterialRatio,
                ProductNumber = ProductNumber,
                SerialNumber = SerialNumber,
                IsDelete = true
            }).ToList();

            var data = await productMaterialRepository.BatchUpdate(entity);
            return data;
        }

        /// <summary>
        /// Bom添加
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> CreateBom(BomInfo entity)
        {
            var data = await bomRepository.AddAsync(entity);
            return data;
        }

        /// <summary>
        /// Bom删除
        /// </summary>
        /// <param name="Id">主键ID</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> DeleteBom(int Id)
        {
            var entity = await bomRepository.GetModelAsync(Id);
            entity.IsDelete = true;
            var data = await bomRepository.Update(entity);
            return data;
        }

        /// <summary>
        /// Bom修改
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>返回受影响行数</returns>
        public Task<int> UpdateBom(BomInfo entity)
        {
            var data = bomRepository.Update(entity);
            return data;
        }
    }
}
